//
//  NewLoginVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2021/3/23.
//  Copyright © 2021 吕晶. All rights reserved.
//

#import "NewLoginVC.h"
#import "NewRegistVC.h"

@interface NewLoginVC ()

@property (nonatomic, strong) UITextField *phoneField;
@property (nonatomic, strong) UITextField *codeField;
@property (nonatomic, strong) UIButton *getCodeBtn;
@property (nonatomic, strong) UIButton *loginBtn;
@property (nonatomic, strong) UIButton *registBtn;

@end

@implementation NewLoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
}

- (void)configUI {
    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(40, NaviHeight + 30, ScreenW - 80, 32)];
    titleLab.text = @"登录";
    titleLab.textColor = HexColor(0x2B2C44);
    titleLab.font = BoldFontOfSize(30);
    [self.view addSubview:titleLab];
    
    UILabel *companyTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(40, titleLab.nim_bottom + 60, titleLab.nim_width, 13)];
    companyTitleLab.text = @"手机号";
    companyTitleLab.font = [UIFont systemFontOfSize:12];
    companyTitleLab.textColor = HexColor(0x2B2C44);
    [self.view addSubview:companyTitleLab];
    
    self.phoneField = [[UITextField alloc] initWithFrame:CGRectMake(40, companyTitleLab.nim_bottom + 2, companyTitleLab.nim_width, 40)];
    self.phoneField.placeholder = @"请输手机号";
    self.phoneField.font = MediumFontOfSize(16);
    self.phoneField.textColor = HexColor(0x2B2C44);
    self.phoneField.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:self.phoneField];
    
    UILabel *line1 = [[UILabel alloc] initWithFrame:CGRectMake(40, self.phoneField.nim_bottom, self.phoneField.nim_width, 1)];
    line1.backgroundColor = HexColor(0xF2F2F5);
    [self.view addSubview:line1];
    
    UILabel *nameTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(40, line1.nim_bottom + 20, titleLab.nim_width, 13)];
    nameTitleLab.text = @"短信验证码";
    nameTitleLab.font = [UIFont systemFontOfSize:12];
    nameTitleLab.textColor = HexColor(0x2B2C44);
    [self.view addSubview:nameTitleLab];
    
    self.codeField = [[UITextField alloc] initWithFrame:CGRectMake(40, nameTitleLab.nim_bottom + 2, nameTitleLab.nim_width, 40)];
    self.codeField.placeholder = @"请输入验证码";
    self.codeField.font = MediumFontOfSize(16);
    self.codeField.textColor = HexColor(0x2B2C44);
    self.codeField.keyboardType = UIKeyboardTypeNumberPad;
    if (@available(iOS 12.0, *)) {
        self.codeField.textContentType = UITextContentTypeOneTimeCode;
    } else {
        self.codeField.textContentType = @"one-time-code";
    }
    [self.view addSubview:self.codeField];
    
    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 102, 40)];
    self.getCodeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, 102, 30)];
    self.getCodeBtn.layer.cornerRadius = 15;
    [self.getCodeBtn setBackgroundColor:HexColorA(0x3D68EB, 0.1)];
    [self.getCodeBtn setTitleColor:HexColor(0x3D68EB) forState:0];
    [self.getCodeBtn setTitle:@"获取验证码" forState:0];
    self.getCodeBtn.titleLabel.font = BoldFontOfSize(14);
    [self.getCodeBtn addTarget:self action:@selector(getCodeClick) forControlEvents:UIControlEventTouchUpInside];
    [rightView addSubview:self.getCodeBtn];
    self.codeField.rightView = rightView;
    self.codeField.rightViewMode = UITextFieldViewModeAlways;
    
    UILabel *line2 = [[UILabel alloc] initWithFrame:CGRectMake(40, self.codeField.nim_bottom, self.codeField.nim_width, 1)];
    line2.backgroundColor = HexColor(0xF2F2F5);
    [self.view addSubview:line2];
    
    self.loginBtn = [[UIButton alloc] initWithFrame:CGRectMake(40, line2.nim_bottom + 40, line2.nim_width, 44)];
    [self.loginBtn setBackgroundColor:HexColor(0x3D68EB)];
    self.loginBtn.layer.cornerRadius = 6;
    [self.loginBtn setTitle:@"登录" forState:0];
    [self.loginBtn setTitleColor:[UIColor whiteColor] forState:0];
    [self.loginBtn addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.loginBtn];
    
    self.registBtn = [[UIButton alloc] initWithFrame:CGRectMake((ScreenW - 100) * 0.5, self.loginBtn.nim_bottom + 20, 100, 44)];
    [self.registBtn setTitle:@"车主注册" forState:0];
    self.registBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.registBtn setTitleColor:Color_ThemeColor forState:0];
    [self.registBtn addTarget:self action:@selector(registClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.registBtn];
}

- (void)registClick {
    NewRegistVC *vc = [NewRegistVC new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.rt_navigationController pushViewController:vc animated:YES];
}

- (void)getCodeClick {
    if ([FactoryMethod isNull:self.phoneField.text]) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入手机号"];
        return;
    }
    if (![FactoryMethod isPhone:self.phoneField.text]) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入正确的手机号"];
        return;
    }
    [self requestForGetCode];
}

- (void)timeStart{
    [self.codeField becomeFirstResponder];
    self.getCodeBtn.userInteractionEnabled = NO;
    self.codeField.text = @"";
    __block int timeout = 60; //倒计时时间
    /**
     *  gcd异步处理倒计时控件
     */
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                self.getCodeBtn.userInteractionEnabled = YES;
                [self.getCodeBtn setTitle:@"重新获取" forState:UIControlStateNormal];
                [self.getCodeBtn setTitleColor:HexColor(0x3D68EB) forState:UIControlStateNormal];
            });
        }else{
            int seconds = timeout;
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                self.getCodeBtn.userInteractionEnabled = NO;
                NSString *strTime = [NSString stringWithFormat:@"%d S",seconds];
                [self.getCodeBtn setTitle:strTime forState:(UIControlStateNormal)];
                [self.getCodeBtn setTitleColor:HexColor(0x2B2C44) forState:UIControlStateNormal];
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}

- (void)requestForGetCode {
    [MBProgressHUD leShowHUDWithMessage:@"请求中" activity:YES];
    [HttpRequest PostWithApi:LogisticsSupplierGetCode_V_1_0 params:@{@"phone":self.phoneField.text,@"type":@"1"} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"验证码获取成功"];
            [self timeStart];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

- (void)loginClick {
    if ([FactoryMethod isNull:self.phoneField.text]) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入手机号"];
        return;
    }
    if (![FactoryMethod isPhone:self.phoneField.text]) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入正确的手机号"];
        return;
    }
    if ([FactoryMethod isNull:self.codeField.text]) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入验证码"];
        return;
    }
    [MBProgressHUD leShowHUDWithMessage:@"登录中" activity:YES];
    [HttpRequest PostWithApi:LogisticsSupplierLogin_V_1_0 params:@{@"phone":self.phoneField.text,@"sms_code":self.codeField.text} progress:^(NSProgress * _Nonnull progress) {

    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            [MBProgressHUD leShowSuccessHUDAddedTo:MAIN_WINDOW message:@"登录成功" delayOperation:^{
                [User loginSuccessData:responseObject];
            }];
        }
    } fail:^(NSError * _Nonnull error) {

    }];
}

@end
